package com.yonyou.cms.epc.service.graphic.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonyou.cms.epc.client.dto.graphic.GradeInfoDTO;
import com.yonyou.cms.epc.client.entity.graphic.GradeInfoPO;
import com.yonyou.cms.epc.client.entity.graphic.GraphicAssemblyInfoPO;
import com.yonyou.cms.epc.client.entity.graphic.GraphicInfoPO;
import com.yonyou.cms.epc.client.feign.WmsServiceFeign;
import com.yonyou.cms.epc.mapper.file.FileMapper;
import com.yonyou.cms.epc.mapper.graphic.GradeInfoMapper;
import com.yonyou.cms.epc.mapper.graphic.GraphicAssemblyInfoMapper;
import com.yonyou.cms.epc.service.graphic.GradeInfoService;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyx.function.exception.DALException;
import com.yonyou.cyx.function.utils.common.StringUtils;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jxy
 * @since 2022-06-08
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class GradeInfoServiceImpl extends ServiceImpl<GradeInfoMapper, GradeInfoPO> implements GradeInfoService {
    //日志对象
    private static final Logger logger = LoggerFactory.getLogger(GradeInfoServiceImpl.class);
    @Autowired
    GradeInfoMapper gradeInfoMapper;
    @Autowired
    GraphicAssemblyInfoMapper graphicAssemblyInfoMapper;
    @Autowired
    FileMapper fileMapper;

    @Autowired
    WmsServiceFeign wmsServiceFeign;

    /**
     * 分页查询对应数据
     *
     * @param page 分页对象
     * @param queryCondition 查询条件
     * @return com.baomidou.mybatisplus.core.metadata.IPage<java.util.Map<java.lang.String,java.lang.Object>>
     * @author jxy
     * @since 2018/7/22 0022
     */
    @Override
    @SuppressWarnings("unchecked")
    @Transactional(readOnly = true)
    public IPage<Map<String, Object>> selectPageBysql(Page page, Map<String, Object> queryCondition) {
        // 获取总条数
        page.setRecords(gradeInfoMapper.selectPageBySql(page, queryCondition));
        return page;
    }

    /**
     * 根据查询条件返回结果集
     *
     * @param queryCondition 查询条件
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     * @author jxy
     * @since 2018/7/22 0022
     */
    @Override
    @Transactional(readOnly = true)
    public List<Map<String,Object >> selectListBySql(Map<String, Object> queryCondition) {
        return gradeInfoMapper.selectListBySql(queryCondition);
    }

    /**
     * 根据查询条件返回结果集
     *
     * @param id 主键ID
     * @return java.util.List<java.util.Map < java.lang.String , java.lang.Object>>
     * @author jxy
     * @since 2018/7/22 0022
     */
    @Override
    @Transactional(readOnly = true)
    public Map<String, Object> getById(Integer id) {
        GradeInfoPO gradeInfoPo = gradeInfoMapper.selectById(id);
        if(gradeInfoPo!=null){
            return gradeInfoPo.toMaps();
        }else{
            throw new DALException("查询失败,没有符合条件的数据");
        }
    }

    /**
     * 根据DTO 进行数据新增
     *
     * @param gradeInfoDTO 页面DTO
     * @return void
     * @author zhangxianchao
     * @since 2018/7/21 0021
     */
    @Override
    public Map<String,Object> insert(GradeInfoDTO gradeInfoDTO) {
        //对对象进行赋值操作
        GradeInfoPO gradeInfoPo = gradeInfoDTO.transDtoToPo(GradeInfoPO.class);
        //执行插入
        gradeInfoMapper.insert(gradeInfoPo);
        //返回插入的值
        return gradeInfoMapper.selectById(gradeInfoPo.getId()).toMaps();
    }

    /**
     * 根据DTO 及ID 进行数据更新
     *
     * @param id 主键ID
     * @param gradeInfoDTO 页面DTO
     * @return void
     * @author zhangxianchao
     * @since 2018/7/21 0021
     */
    @Override
    public Map<String,Object> update(Integer id, GradeInfoDTO gradeInfoDTO) {
        GradeInfoPO gradeInfoPo = gradeInfoMapper.selectById(id);
        //对对象进行赋值操作
        gradeInfoDTO.transDtoToPo(gradeInfoPo);
        //执行更新
        gradeInfoMapper.updateById(gradeInfoPo);
        return gradeInfoPo.toMaps();
    }

    /**
     * 根据ID 进行删除数据
     *
     * @param id
     * @return void
     * @author zhangxianchao
     * @since 2018/8/25 0025
     */
    @Override
    public void deleteById(Integer id) {
        int deleteCount = gradeInfoMapper.deleteById(id);
        logger.debug("删除数量：{}",deleteCount);
        if (deleteCount<=0) {
            throw new DALException("删除失败,没有符合条件的数据");
        }
    }

    /**
     * 根据ids 进行删除
     *
     * @param ids
     * @return void
     * @author zhangxianchao
     * @since 2018/8/25 0025
     */
    @Override
    public void deleteBatchIds(String ids) {
        List<Integer> longList = StringUtils.convertStrToArray(ids,",", Integer.class);
        int deleteCount = gradeInfoMapper.deleteBatchIds(longList);
        logger.debug("删除数量：{}",deleteCount);
        if (deleteCount!=longList.size()) {
            throw new DALException("删除失败,没有符合条件的数据");
        }
    }

    @Override
    public List<GraphicInfoPO> queryModel(Map<String, Object> params) {
        return gradeInfoMapper.queryModel(params);
    }

    @Override
    public List<GraphicInfoPO> querySystem(Map<String, Object> map) {
        return gradeInfoMapper.querySystem(map);
    }
    @Override
    public List<GraphicInfoPO> queryGraphicInFo(Map<String, Object> map) {
        List<GraphicInfoPO> graphicInfoPos = gradeInfoMapper.queryGraphicInFo(map);
        return graphicInfoPos;
    }

    @Override
    public List<GraphicAssemblyInfoPO> queryGraphicAssemblyInFo(Map<String, Object> map) {
        return gradeInfoMapper.queryGraphicAssemblyInFo(map);
    }

    @Override
    public List<GraphicAssemblyInfoPO> queryParts(GraphicAssemblyInfoPO graphicAssemblyInfoPo) {
//        LambdaUpdateWrapper<GraphicAssemblyInfoPO> wrapper = new LambdaUpdateWrapper<>();
//        if (graphicAssemblyInfoPo.getParentId()==null){
//            wrapper.eq(GraphicAssemblyInfoPO::getGraphicId,graphicAssemblyInfoPo.getGraphicId());
//            wrapper.eq(GraphicAssemblyInfoPO::getParentId,0);
//        }else {
//            if(graphicAssemblyInfoPo.getIsSpare()==10041001){
//                wrapper.eq(GraphicAssemblyInfoPO::getGraphicId,graphicAssemblyInfoPo.getGraphicId());
//                wrapper.eq(GraphicAssemblyInfoPO::getAssemblyId,graphicAssemblyInfoPo.getAssemblyId());
//            }else {
//                wrapper.eq(GraphicAssemblyInfoPO::getGraphicId,graphicAssemblyInfoPo.getGraphicId());
//                wrapper.eq(GraphicAssemblyInfoPO::getParentId,graphicAssemblyInfoPo.getAssemblyId());
//            }
//
//        }
        List<GraphicAssemblyInfoPO> graphicAssemblyInfoPos = gradeInfoMapper.partsList(graphicAssemblyInfoPo);
        return graphicAssemblyInfoPos;
    }

    @Override
    public boolean insertAndUpdate(Integer assemblyId,List<GradeInfoPO> list) {
        LoginInfoDto loginInfo = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        if (gradeInfoMapper.delGradeInFo(assemblyId)>0){
            logger.debug("删除数量：{}",assemblyId);
        }
        if(list!=null&&list.size()>0){
            for (int i=0;i<list.size();i++){
                //填充数据
                list.get(i).setOwnerParCode(loginInfo.getOwnerParCode());
                list.get(i).setOwnerCode(loginInfo.getOwnerCode());
                list.get(i).setOrgId(loginInfo.getOrgId().toString());
                list.get(i).setCompanyCode(loginInfo.getCompanyCode());
                list.get(i).setCreatedBy(loginInfo.getUserId().toString());
                list.get(i).setUpdatedBy(loginInfo.getUserId().toString());
                list.get(i).setCreatedAt(LocalDateTime.now());
                list.get(i).setUpdatedAt(LocalDateTime.now());
                list.get(i).setAppId(loginInfo.getAppId());
            }
            if (gradeInfoMapper.insertAndUpdate(list)>0){
                return true;
            }
            throw new DALException("保存失败");
        }
        return true;
    }

    @Override
    public List<GraphicAssemblyInfoPO> queryPartsInFo(String graphicCode,String graphicId) {
        return gradeInfoMapper.queryPartsInFo(graphicCode,graphicId);
    }

    @Override
    public List<Map<String, Object>> getfilelist(Map<String, Object> map) {
        LoginInfoDto loginInfo = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        map.put("appId",loginInfo.getAppId());
        List<Map<String, Object>> maps = fileMapper.selectFilesByDjid(map);
        return maps;
    }

    @Override
    public List<Map<String, Object>> getSubPartList(Map<String, Object> queryCondition) {
        List<Map<String, Object>> data = wmsServiceFeign.getSubPartList(queryCondition).getData();
        return data;
    }
}
